package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.ServiceInventoryPageQuery;
import com.eastfair.venueservice.dto.ServiceInventoryQuery;
import com.eastfair.venueservice.dto.ServiceInventorySaveDTO;
import com.eastfair.venueservice.dto.ServiceInventoryUpdateDTO;
import com.eastfair.venueservice.entity.ServiceInventory;
import com.eastfair.venueservice.service.ServiceInventoryService;
import com.eastfair.venueservice.vo.ServiceInventoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 服务库存
 * </p>
 *
 * @author linan
 * @date 2022-07-14
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceInventory")
@Api(value = "ServiceInventory", tags = "服务库存")
@PreAuth(replace = "venueservice:serviceInventory:")
public class ServiceInventoryController extends SuperController<ServiceInventoryService, Long, ServiceInventory, ServiceInventoryPageQuery, ServiceInventorySaveDTO, ServiceInventoryUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ServiceInventory> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ServiceInventory> serviceInventoryList = list.stream().map((map) -> {
            ServiceInventory serviceInventory = ServiceInventory.builder().build();
            //TODO 请在这里完成转换
            return serviceInventory;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(serviceInventoryList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServiceInventoryPageQuery> params) {
        ServiceInventoryPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 查询库存列表
     * @param serviceInventoryQuery
     * @return
     */
    @ApiOperation(value = "查询库存列表")
    @PostMapping(value = "/queryInventoryList")
    public R<Page<ServiceInventoryVO>> queryInventoryList(@RequestBody PageParams<ServiceInventoryQuery> serviceInventoryQuery){
        try {
            return R.success(baseService.queryInventoryList(serviceInventoryQuery));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }



    /**
     * 查询库存统计
     * @param serviceInventoryQuery
     * @return
     */
    @ApiOperation(value = "查询库存统计")
    @PostMapping(value = "/queryInventoryStatistics")
    public R<Map<String,Object>> queryInventoryStatistics(@RequestBody ServiceInventoryQuery serviceInventoryQuery){
        try {
            return R.success(baseService.queryInventoryStatistics(serviceInventoryQuery));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

}
